#include "Graphic.h"
Graphic::Graphic()
{
	sm_d3d=NULL;
	sm_d3d_device=NULL;
}
bool Graphic::InitDevice(HWND p_handle)
{
	if (NULL==(sm_d3d = Direct3DCreate9(D3D_SDK_VERSION)))
	{
		return false;
	}
	ZeroMemory( &sm_d3d_pp, sizeof(sm_d3d_pp) );
	sm_d3d_pp.Windowed = TRUE;
	sm_d3d_pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	sm_d3d_pp.BackBufferFormat = D3DFMT_UNKNOWN;
	sm_d3d_pp.BackBufferCount  = 1;
	sm_d3d_pp.BackBufferHeight = SCREEN_HEIGHT;
	sm_d3d_pp.BackBufferWidth  = SCREEN_WIDTH;
	sm_d3d_pp.hDeviceWindow    = p_handle;
	if( FAILED( sm_d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,p_handle,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&sm_d3d_pp, &sm_d3d_device ) ) )
	{
		if( FAILED( sm_d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, p_handle,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING,
			&sm_d3d_pp, &sm_d3d_device ) ) )
		{
			if( FAILED( sm_d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_SW, p_handle,
				D3DCREATE_SOFTWARE_VERTEXPROCESSING,
				&sm_d3d_pp, &sm_d3d_device ) ) )
			{
				return false;
			}
		}
	}
	return true;
}
LPDIRECT3DDEVICE9 Graphic::GetDevice()
{
	return sm_d3d_device;
}
LPDIRECT3D9 Graphic::GetD3D()
{
	return sm_d3d;
}
D3DPRESENT_PARAMETERS Graphic::GetD3DPP()
{
	return sm_d3d_pp;
}
void Graphic::Release()
{
	if(sm_d3d!=NULL)
	{
		sm_d3d->Release();
		sm_d3d=NULL;
	}
	if(sm_d3d_device!=NULL)
	{
		sm_d3d_device->Release();
		sm_d3d_device=NULL;
	}
}
void Graphic::BeginRender()
{
	if (sm_d3d_device!=NULL)
	{
		sm_d3d_device->BeginScene();
		sm_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0);
	}
}
void Graphic::EndRender()
{
	sm_d3d_device->EndScene();
	sm_d3d_device->Present(NULL, NULL, NULL, NULL);
}


